package com.jdbc;

import java.sql.*;
import java.util.LinkedList;
import java.util.Queue;

public class Cat {
    public static void main(String[] args) {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db4?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8", "root", "Yuanwutong07");
            stmt = conn.createStatement();


            //增 返回值为操作行数
            /*String insertSql = "insert into cat(id,nickname,weight,birth) values(3,'ty','10','2020-07-16')";
            int count=stmt.executeUpdate(insertSql);*/
            //删 返回值为操作行数
            /*String deleteSql="delete from cat where id=3";
            int count2= stmt.executeUpdate(deleteSql);*/
            //改 返回值为操作行数
            /*String updateSql="update cat set nickname='alpha',weight='24' where id='2' ";
            int count3=stmt.executeUpdate(updateSql);*/
            //查
            String checkSql="select id,nickname,weight,birth from cat";
            rs = stmt.executeQuery(checkSql);
            boolean has= rs.next();
            while (has){
                String id=rs.getString(1);
                String nickname=rs.getString(2);
                String weight=rs.getString(3);
                String birth=rs.getString(4);
                System.out.println(id+","+nickname+","+weight+","+birth);
                has= rs.next();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try{
                    conn.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }
    }
}